﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;
using YJKTextDimension.Commands;

namespace YJKTextDimension.Forms
{
	public partial class LabelLevelForm : System.Windows.Forms.Form
	{
		public string LabelStyle
		{
			get
			{
				return this.cmbAlignDimType.SelectedItem.ToString();
			}
		}

		public int LabelMode
		{
			get
			{
				return this.m_nLabelMode;
			}
		}

		public LabelLevelForm(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.InitLabelStyle();
			base.StartPosition = FormStartPosition.Manual;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
				this.cmbAlignDimType.SelectedIndex = 0;
				this.m_nLabelMode = 0;
				this.rbBoth.Checked = true;
			}
		}

		private void InitLabelStyle()
		{
			List<DimensionType> dimensionTypes = Common.GetDimensionTypes(this.m_doc);
			this.cmbAlignDimType.Items.Add("HY-轴线标注");
			foreach (DimensionType dimensionType in dimensionTypes)
			{
				if (dimensionType.StyleType == null && !this.cmbAlignDimType.Items.Contains(dimensionType.Name) && "线性尺寸标注样式" != dimensionType.Name)
				{
					this.cmbAlignDimType.Items.Add(dimensionType.Name);
				}
			}
		}

		private void rbBoth_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nLabelMode = 0;
		}

		private void rbTotal_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nLabelMode = 1;
		}

		private void rbSingle_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nLabelMode = 2;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			LabelLevelForm.SendMessage(CmdLabelLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			LabelLevelForm.keybd_event(bVk, 0, dwFlags, 0);
			LabelLevelForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void LabelLevelForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void LabelLevelForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void LabelLevelForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				LabelLevelForm.SendMessage(CmdLabelLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void LabelLevelForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "LabelLevel.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.cmbAlignDimType.Text);
			streamWriter.WriteLine(this.m_nLabelMode);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "LabelLevel.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
                base.Location = new System.Drawing.Point(x, y);
				string value = streamReader.ReadLine();
				int num = this.cmbAlignDimType.Items.IndexOf(value);
				this.cmbAlignDimType.SelectedIndex = ((num > 0) ? num : 0);
				string s = streamReader.ReadLine();
				this.m_nLabelMode = int.Parse(s);
				if (this.m_nLabelMode == 0)
				{
					this.rbBoth.Checked = true;
				}
				else if (1 == this.m_nLabelMode)
				{
					this.rbTotal.Checked = true;
				}
				else
				{
					this.rbSingle.Checked = true;
				}
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		public Document m_doc;

		private int m_nLabelMode;
	}
}
